/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import types.TableTyp;

/**
 * Dummy implementation of LazyDataModel that uses a list to mimic a real
 * datasource like a database.
 */
public class Lazy extends LazyDataModel<TableTyp> {

	private static final long serialVersionUID = 8215114457209842727L;
	private List<TableTyp> datasource;

	public Lazy(List<TableTyp> datasource) {
		this.datasource = new ArrayList<TableTyp>();
		this.datasource = datasource;
	}
	
	@Override
	public TableTyp getRowData(String rowKey) {
		for (TableTyp post : datasource) {
			if (Integer.toString(datasource.indexOf(post) + 1).equals(rowKey))
				return post;
		}

		return null;
	}

	@Override
	public Object getRowKey(TableTyp post) {
		return Integer.toString(datasource.indexOf(post) + 1);
	}

	@Override
	public void setRowIndex(int rowIndex) {
		if (rowIndex == -1 || getPageSize() == 0) {
			super.setRowIndex(-1);
		} else {
			super.setRowIndex(rowIndex % getPageSize());
		}
	}

	@Override
	public List<TableTyp> load(int first, int pageSize, String sortField,
			SortOrder sortOrder, Map<String, String> filters) {
		List<TableTyp> data = new ArrayList<TableTyp>();

		// filter
		for (TableTyp post : datasource) {
			boolean match = true;

			for (Iterator<String> it = filters.keySet().iterator(); it
					.hasNext();) {
				try {
					String filterProperty = it.next();
					String filterValue = filters.get(filterProperty);
					String fieldValue = String.valueOf(post.getClass()
							.getField(filterProperty).get(post));

					if (filterValue == null
							|| fieldValue.startsWith(filterValue)) {
						match = true;
					} else {
						match = false;
						break;
					}
				} catch (Exception e) {
					match = false;
				}
			}

			if (match) {
				data.add(post);
			}
		}

		// rowCount
		int dataSize = data.size();
		this.setRowCount(dataSize);

		// paginate
		if (dataSize > pageSize) {
			try {
				return data.subList(first, first + pageSize);
			} catch (IndexOutOfBoundsException e) {
				return data.subList(first, first + (dataSize % pageSize));
			}
		} else {
			return data;
		}
	}
}
